package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.sunlight.interneteverything.R;

import java.util.Random;

/**
 * Created by SunLight on 2017/8/9.
 */

public class BtnView extends View {
    Bitmap bgoff_right, bgon_right,bgoff_left,bgon_left;
    Resources resources;
    UpDown upDown;
    boolean ViewSate = false;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    BtnView.this.invalidate();
                    break;
            }
        }
    };
    TypedArray t;
    String text,judgeImg;
    float textSize;
    float left,top;
    int textColor;

    public BtnView(Context context) {
        super(context);
        init();
    }
    public void init() {
        resources = getResources();
        bgoff_right = ((BitmapDrawable) resources.getDrawable(R.drawable.off_right)).getBitmap();
        bgon_right = ((BitmapDrawable) resources.getDrawable(R.drawable.on_right)).getBitmap();
        bgoff_left=((BitmapDrawable) resources.getDrawable(R.drawable.off_left)).getBitmap();
        bgon_left=((BitmapDrawable) resources.getDrawable(R.drawable.on_left)).getBitmap();
        upDown = new UpDown();
        upDown.start();
        if(new Random().nextInt(2)==0){
            sate=true;
        }else{
            sate=false;
        }

    }
    public void init(AttributeSet attrs){
        t=getContext().obtainStyledAttributes(attrs,R.styleable.BtnView);
        text=t.getString(R.styleable.BtnView_textValue);
        judgeImg=t.getString(R.styleable.BtnView_judgeImg);
        textSize=t.getDimension(R.styleable.BtnView_textSize,34);
        textColor=t.getColor(R.styleable.BtnView_textColor,0xFFFFFF);
        left=t.getDimension(R.styleable.BtnView_margin_left,5);
        top=t.getDimension(R.styleable.BtnView_margin_top,5);
        t.recycle();
    }

    public BtnView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        init(attrs);

    }

    public BtnView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        init(attrs);
    }

    int colors =Color.argb(255,19,113,107);

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(0,10);

        Paint paint1=new Paint();
        paint1.setStrokeWidth(3);
        paint1.setAntiAlias(true);
        paint1.setColor(colors);
        if(judgeImg.equals("left")){
            drawLins(canvas,paint1,text);
            if (ViewSate) {
                canvas.drawBitmap(bgon_left, left, size+top, null);
            } else {
                canvas.drawBitmap(bgoff_left, left, size+top, null);
            }
            drawText(canvas,320);
        }else if(judgeImg.equals("right")){
            drawLins(canvas,paint1,text);
            if (ViewSate) {
                canvas.drawBitmap(bgon_right, left, size+top, null);
            } else {
                canvas.drawBitmap(bgoff_right, left, size+top, null);
            }
            drawText(canvas,255);

        }

    }
    public void drawText(Canvas canvas,int num){
        Rect targetRect = new Rect((int) left, (int) (size+top), (int) (num+left), (int) (73  + size+top));
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(3);
        paint.setTextSize(textSize);
        paint.setColor(Color.TRANSPARENT);
        canvas.drawRect(targetRect, paint);
        paint.setColor(Color.WHITE);
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(text, targetRect.centerX(), baseline, paint);
    }


    public void drawLins(Canvas canvas,Paint paint,String text){
        switch (text){
            case "环境感知":
                canvas.drawLine(left+285,37+size+top,655+left,200+top,paint);
                break;
            case "新农业":
                canvas.drawLine(285+left,37+size+top,475+left,37+top,paint);
                break;
            case "安全识别":
                canvas.drawLine(285+left,37+size+top,500+left,100,paint);
                break;

            case "健康管理":
                canvas.drawLine(left,37+size+top,0,150+top,paint);
                break;
            case "交通调度":
                canvas.drawLine(left,37+size+top,0,37+top,paint);
                break;
            case "智慧仓储":
                canvas.drawLine(left,37+size+top,-50,100,paint);
                break;
        }

    }

    boolean sate = false;
    int size = 0;

    public OnClicking onClicking;

    public static interface OnClicking{
        void OnClik(String text);
    }

    public void setOnClicking(OnClicking onClicking) {
        this.onClicking = onClicking;
    }

    class UpDown extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(60);
                    if (sate == false) {
                        if (size <= 5) {
                            size++;
                        } else {
                            sate = true;
                        }
                    } else {
                        if (size >= -5) {
                            size--;
                        } else {
                            sate = false;
                        }
                    }
                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                ViewSate = false;
                onClicking.OnClik(text);
                break;
            case MotionEvent.ACTION_DOWN:
                float x=event.getX();
                float y=event.getY();
                if(x>left&&x<left+300){
                    if(y>top+size&&y<top+size+73){
                        ViewSate = true;
                    }
                }
                break;
        }
        return true;
    }


}
